package cn.itcast.mq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * ClassName: PublisherTest
 * Package: cn.itcast.mq
 * Description:
 *
 * @Author BuTang
 * @Create 2023/12/11 22:32
 * @Version 1.0
 */
public class PublisherTest {

    @Test
    public void testSendMessage() throws Exception {
        //建立链接
        ConnectionFactory factory = new ConnectionFactory();

        //设置链接参数，分别是主机名，端口名，vhost，用户名，密码
        factory.setHost("121.43.58.63");
        factory.setPort(5672);
        factory.setVirtualHost("/");
        factory.setUsername("butang");
        factory.setPassword("zrc990416");

        //1.2建立链接
        Connection connection = factory.newConnection();
        //创建通道Channel
        Channel channel = connection.createChannel();

        String queueName = "simple.queue";
        channel.queueDeclare(queueName, false, false, false, null);

        String msg = "Hello,rabbitMQ!";
        channel.basicPublish("",queueName,null,msg.getBytes());
        System.out.println("发送消息成功：【"+msg+"】");
        channel.close();
        connection.close();

    }

}
